package com.example.work;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.AnimationUtils;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.ViewSwitcher;

import androidx.appcompat.app.AppCompatActivity;

public class ImageSwitcha_Activity extends Activity {
    private  int[]  arrayPicture=new int[]{R.drawable.pa,R.drawable.pb,R.drawable.pc
           };
    private ImageSwitcher imageSwitcher;
    private int  index = 0;
    private  float touchDownX;
    private  float touchUpX;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.img_index);
        //设置全屏显示

        imageSwitcher=findViewById(R.id.img);
        //设置视图工厂
        imageSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
            @Override
            public View makeView() {
                ImageView  imageView=new ImageView(ImageSwitcha_Activity.this);
                imageView.setImageResource(arrayPicture[index]);//设置显示图片（利用下标）
                return imageView;//返回图像视图
            }
        });
        //设置触摸监听器
        imageSwitcher.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                //判断动作是不是按下  获得按下时的X坐标
                if(event.getAction()==MotionEvent.ACTION_DOWN) {
                    touchDownX=event.getX();
                    return true;
                } else if(event.getAction()==MotionEvent.ACTION_UP) {
                    touchUpX=event.getX();
                    //判断是左滑动还是右滑动
                    if(touchUpX-touchDownX>100){
                        index--;
                        //判断是不是第一张图片 是就将索引变成最后一张图片索引，
                        // 不是则当前索引减一
                        if(index == -1){
                            Toast.makeText(ImageSwitcha_Activity.this,
                                    "请向右翻动哦～", Toast.LENGTH_SHORT)
                                    .show();
                            index = 0;
                            return false;
                        }
                        else{
                            //使用自带的淡入淡出
                            imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(ImageSwitcha_Activity.this,android.R.anim.fade_in));
                            imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(ImageSwitcha_Activity.this,android.R.anim.fade_out));
                            imageSwitcher.setImageResource(arrayPicture[index]);
                        }
                    }else if(touchDownX-touchUpX>100){
                        index++;
                        if(index == arrayPicture.length){
                            Intent intent=new Intent(ImageSwitcha_Activity.this,login.class);
                            startActivity(intent);
                        }
                        else{
                            System.out.println(index);
                            imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(ImageSwitcha_Activity.this,android.R.anim.fade_in));
                            imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(ImageSwitcha_Activity.this,android.R.anim.fade_out));
                            imageSwitcher.setImageResource(arrayPicture[index]);
                        }
                    }
                    return true;
                }
                return false;
            }
        });
    }
}
